<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="患者姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医生姓名" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入处方医生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处方描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入处方描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mop:prescription:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mop:prescription:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mop:prescription:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mop:prescription:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prescriptionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="处方ID" align="center" prop="id"/>
      <el-table-column label="患者姓名" align="center" prop="patientName"/>
      <el-table-column label="身份证号" align="center" prop="idNumber"/>
      <el-table-column label="处方医生" align="center" prop="doctorName"/>
      <el-table-column label="处方描述" align="center" prop="description"/>
      <el-table-column label="处方药品" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleListMop(scope.row)"
            v-hasPermi="['mop:prescription:getMopList']"
          >编辑
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="收费" align="center" prop="amount"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mop:prescription:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mop:prescription:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看处方药品列表   -->
    <el-dialog :title="titleMop" :visible.sync="openMop" width="1200px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAddMop"
            v-hasPermi="['mop:prescription:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDeleteMop"
            v-hasPermi="['mop:prescription:delMop']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['mop:prescription:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="mopList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="药品名称" align="center" prop="medicineName"/>
        <el-table-column label="图片" align="center" prop="medicineImg">
          <template slot-scope="scope">
            <image-preview :src="scope.row.medicineImg" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="单价" align="center" prop="price"/>
        <el-table-column label="数量" align="center" prop="number" />
<!--        <el-table-column label="数量" align="center" prop="number">-->
<!--          <template slot-scope="scope">-->
<!--            <el-input-number v-model="scope.row.number" @change="handleChange(scope.row)" label="描述文字"></el-input-number>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="总计" align="center" prop="amount"/>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  新增处方药品的窗口-->
    <el-dialog :title="titleAddMop" :visible.sync="openAddMop" width="1000px" append-to-body>
      <el-tabs :tab-position="tabPosition" style="height: 400px;overflow-y: auto">
        <el-tab-pane v-for="category in categoryList" :label="category.name">
          <el-table v-loading="loading" :data="category.medicineList">
            <el-table-column label="药品名称" align="center" prop="name"/>
            <el-table-column label="图片" align="center">
              <template slot-scope="scope">
                <image-preview :src="scope.row.image" :width="50" :height="50"/>
              </template>
            </el-table-column>
            <el-table-column label="单价" align="center" prop="price"/>
            <el-table-column label="数量" align="center" prop="number">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.number" @change="handleChange(scope.row)" label="描述文字" :min="0"></el-input-number>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addMop">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改处方对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="患者姓名" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入患者ID"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="医生姓名" prop="doctorId">
          <el-input v-model="form.doctorName" placeholder="请输入处方医生ID"/>
        </el-form-item>
        <el-form-item label="医生工号" prop="doctorId">
          <el-input v-model="form.doctorId" placeholder="请输入处方医生ID"/>
        </el-form-item>
        <el-form-item label="处方描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入处方描述(拿了哪些药)"/>
        </el-form-item>
        <el-form-item label="收费" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入收费"/>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addMops,
  listComs,
  getMopList,
  listPrescription,
  getPrescription,
  delPrescription,
  addPrescription,
  updatePrescription, delMop
} from "@/api/mop/prescription";
import log from "@/views/monitor/job/log";

export default {
  name: "Prescription",
  data() {
    return {
      //存储点击“编辑”后的处方id
      pid: 1,
      //存挑选药品的过渡数组
      medicinesList: [],
      //新增窗口的方向
      tabPosition: 'left',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处方表格数据
      prescriptionList: [],
      // 处方药品列表数据
      mopList: [],
      // 弹出层标题
      title: "",
      //处方包含药品弹出框的标题
      titleMop: "",
      // 是否显示弹出层
      open: false,
      //处方药品列表的是否显示弹出层
      openMop: false,
      //处方药品新增窗口的是否显示弹出层
      openAddMop: false,
      // 查询参数
      //新增处方药品时的药品分类
      categoryList: [
        {
          name: 'zzz',
          medicineList: []
        }
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientId: null,
        patientName: null,
        idNumber: null,
        doctorId: null,
        doctorName: null,
        description: null,
        amount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patientId: [
          {required: true, message: "患者ID不能为空", trigger: "blur"}
        ],
        doctorId: [
          {required: true, message: "处方医生ID不能为空", trigger: "blur"}
        ],
        description: [
          {required: true, message: "处方描述(拿了哪些药)不能为空", trigger: "blur"}
        ],
        amount: [
          {required: true, message: "收费不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 获取药品分类及该分类下的所有药品列表 */
    getComsList() {
      listComs().then(response => {
        this.categoryList = response.data;
        this.loading = false;
      });
    },
    /** 查询处方列表 */
    getList() {
      this.loading = true;
      listPrescription(this.queryParams).then(response => {
        this.prescriptionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openMop = false;//关闭处方药品框
      this.openAddMop = false;//关闭给处方添加药品窗口
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        patientId: null,
        idNumber: null,
        doctorId: null,
        description: null,
        amount: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加处方";
    },
    /** 新增处方药品按钮操作 */
    handleAddMop() {
      this.openAddMop = true;
      this.getComsList();//获取所有药品数据列表
      this.titleAddMop = "添加处方药品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      //用或运算符,第一个为真则值为第一个,否则才看第二个(此处的ids也是选一个的时候可以修改,数据元素只有一个)
      const id = row.id || this.ids//row.id是本id,ids是多项选中的id数组
      getPrescription(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改处方";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePrescription(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.openMop = false;//关闭处方药品框
              this.getList();
            });
          } else {
            addPrescription(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除处方编号为"' + ids + '"的数据项？').then(function () {
        return delPrescription(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 删除处方药品按钮操作 */
    handleDeleteMop(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
        return delMop(ids);
      }).then(() => {
        this.getComsList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mop/prescription/export', {
        ...this.queryParams
      }, `prescription_${new Date().getTime()}.xlsx`)
    },

    /** 查看处方药品按钮操作 */
    handleListMop(row) {
      const id = row.id//此处不需要ids数组
      this.pid = id;//存到变量中便于后面添加药品的操作
      getMopList(id).then(response => {
        this.mopList = response.data;
        this.openMop = true;
        this.titleMop = "处方药品";
      });
    },
    /** 处理处方药品添加数量，存到数组的操作的操作 */
    handleChange(row) {
      const index = this.medicinesList.findIndex(item=>item.id == row.id);
      if (index != -1){
        //找到id相同的,进行修改赋值,number=0需要删除
        if (row.number == 0){
          this.medicinesList.splice(index, 1);//删除index位置上的1个数据
        }else {
          this.$set(this.medicinesList,index,row);
        }
      }else {
        //没有id相同的,进行添加即可
        this.medicinesList.push(row);
      }
      console.log(this.medicinesList)
    },

    /** 已经添加好药品后新增到 mop表的操作 */
    addMop(){
      addMops(this.medicinesList,this.pid).then(response => {
        this.mopList = response.data;
        this.openAddMop = false
      });
    }
  }
};
</script>
